define(
		[ "PDAppDir/properties", "PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar", "PDUtilDir/inputSelect", "PDUtilDir/fileupload/multiFileUpload", "PDUtilDir/validator", "Date", "DateCN", "css!DateCss", "PicView", "css!picViewCss" ],
		function(Properties, Util, Tool, Slidebar, inputSelect, MFU, validator) {
			function updateSupportInit($scope, $compile){
				return new updateSupport($scope, $compile);
			};
			function updateSupport($scope, $compile){
				this.$scope = $scope;
				this.$compile = $compile;
				this.validator = validator;
			};
			updateSupport.fn = updateSupport.prototype;
			
			updateSupport.fn.init = function(data){
				this.entity = data;
				this.bind = {
					path: Properties.URL_FILE_SERVER_URL,
					images: new Array()
				};
			};
			
			updateSupport.fn.digest = function(){
				 this.$scope.$digest();
			};
			updateSupport.fn.run = function(data) {
			
				var _this = this;
				_this.init(data);
				this.slidebars = Slidebar({
					size : 'lg',
					url : getServer()
							+ "/static/app/register/personInfos/views/update.html",
//					close : true,
					$scope: this.$scope,
					$compile: this.$compile,
					afterLoad : function($Panel) {
						_this.$scope.entity.personNo = data.personNo;
						_this.$scope.entity.id = data.id;
						_this.$scope.entity.extend1 = data.extend1;
						_this.$scope.entity.extend2 = data.extend2;
						_this.$scope.entity.extend3 = data.extend3;
						_this.$scope.entity.extend4 = data.extend4;
						_this.$scope.entity.extend5 = data.extend5;
						_this.$scope.entity.extend6 = data.extend6;
						_this.$scope.entity.extend7 = data.extend7;
						_this.digest();
						$("#name").val(data.name);
						$("#folk").val(data.folk);
						$("#sex").val(data.sex);
						$("#birthDay").val(data.birthDay);
						$("#cardsn").val(data.cardSn);
						$("#address").val(data.address);
						$("#agency").val(data.agency);
						$("#valid").val(data.validDuring);
						$("#samid").val(data.samid);
						$("#idCardSN").val(data.idCardSn);
						$("#JPGPhotobuf").attr('src',data.jpgPhotobuf);
						$("#capturePhoto").attr('src',data.jpgHead);
						var projectCodes = data.projectCode.split(",");
						
						
			/*			inputSelect.dropCache();
						inputSelect({
							id : "projectName",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : _this.$scope.cache.projects,
							simpleData : true,
							// 启用文本搜索
							initData:projectCodes,
							searchAble : false,
							callback : function(id) {
								_this.entity.certificates = id[0];
								_this.$scope.updateVisitorForm.certificates.$setViewValue(_this.entity.certificates);
							}
						});*/
						inputSelect.dropCache();
						inputSelect({
							id : "projectNameA",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							   multi : false,
							data : _this.$scope.cache.projects,
							simpleData : true,
							// 启用文本搜索
							 initData:data.projectCode.split(','),
							searchAble : false,
							callback : function(id,data) {
								  var str = "";
							    $.each(data, function(i, n){
				                    str += n["dictCode"] + ",";
				                })
				                
								_this.$scope.entity.projectCode = id[0];
							    _this.$scope.entity.projectCodes = str;
							    _this.$scope.entity.projectName=id[1];
								
								
							}
						});
						//队伍
						inputSelect.dropCache();
						inputSelect({
							id : "teamName",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : _this.$scope.cache.teams,
							simpleData : true,
							// 启用文本搜索
							 initData:[data.team],
							searchAble : false,
							callback : function(id) {
								_this.$scope.entity.teamCode = id[0];
								_this.$scope.entity.teamName = id[1];
								_this.$scope.entity.team=id[0];
							}
						});
						
						inputSelect.dropCache();
						inputSelect({
							id : "extend3Name",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : _this.$scope.cache.works,
							simpleData : true,
							// 启用文本搜索
							 initData:[data.extend3],
							searchAble : false,
							callback : function(id) {
								_this.$scope.entity.extend3 = id[0];
								_this.$scope.entity.extend3Name = id[1];
							}
						});
						
						inputSelect.dropCache();
						inputSelect({
							id : "roleName",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : _this.$scope.cache.roles,
							simpleData : true,
							// 启用文本搜索
							 initData:[data.personRole],
							searchAble : false,
							callback : function(id) {
								_this.$scope.entity.roleCode = id[0];
								_this.$scope.entity.roleName = id[1];
							}
						});
						
						inputSelect.dropCache();
						inputSelect({
							id : "workerTypeName",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data :  _this.$scope.cache.workerType,
							simpleData : true,
							// 启用文本搜索
							 initData:[data.workerType],
							searchAble : false,
							callback : function(id) {
								_this.$scope.entity.workerType = id[0];
								_this.$scope.entity.workerTypeName = id[1];
							}
						});
						
					}
				});
			};
			
			
			updateSupport.fn.initialization = function(){
				this.baseInit();
			};
			updateSupport.fn.baseInit = function() {
				var _this = this;
				//加载code
/*				$.ajax({
					url : getServer()
							+ "/flea/hclv/visitor/selectVisitorImage.htmls",
					method : 'get',
					data: {
						visitorId: _this.entity.id
					},
					success : function(data) {
//						var rows = data.data, len = rows.length, obj = undefined;
//						for(var i = 0; i < len; i++){
//							obj = $.extend(rows[i], {
//								path: Properties.URL_FILE_SERVER_URL + rows[i].path
//							});
//							_this.bind.images.push(obj);
//						}
						_this.bind.images = data.data
						_this.$scope.updateVisitorForm.isOk.$setViewValue(true);
						_this.digest();
					}
				});*/
				//证件类型
				inputSelect.dropCache();
				inputSelect({
					id : "certificatesName",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.certificates,
					simpleData : true,
					// 启用文本搜索
					initData:[ _this.entity.certificates ],
					searchAble : false,
					callback : function(id) {
						_this.entity.certificates = id[0];
						_this.$scope.updateVisitorForm.certificates.$setViewValue(_this.entity.certificates);
					}
				});
				//类型
				inputSelect.dropCache();
				inputSelect({
					id : "typeName",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.types,
					simpleData : true,
					// 启用文本搜索
					initData:[_this.entity.type],
					searchAble : false,
					callback : function(id) {
						_this.entity.type = id[0];
						_this.$scope.updateVisitorForm.type.$setViewValue(_this.entity.type);
					}
				});
				$("#birthDate").datetimepicker({
		            //设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
		            language : "cn",
		            //输出格式化
		            format : 'yyyy-mm-dd',
		            //直接选择‘今天’
		            todayBtn : true,
		            //设置最精确的时间选择视图
		            minView : 'month',
		            //高亮当天日期
		            todayHighlight : true,
		            //选择完毕后自动关闭
		            autoclose : true
		        });
				$("#birthDatete").datetimepicker('setDate', new Date(_this.entity.birthDate) );
				MFU.init({
	                placeAt:"license",
//	                allowPhyDelete:true,
	                allowDelete:true,
	                allowMC:true,
	                onUploadSuccess:function(file){
	                	$.each(_this.bind.images, function(index, row){
	                    	if(row.key == file.name){
	                    		row = $.extend(row, {
	                    			resouceId: file.swordFileId,
	                    			path: file.swordFilePath
	                    		});
	                    	}
	                    });
	                	_this.$scope.updateVisitorForm.isOk.$setViewValue(true);
	                },
	                onDeleteSavedFile:function(file){
	                	_this.removeImg(file.swordFileId);
	                    _this.digest();
	                },
	                onFilesQueued: function(files){
	                	var webUploader = this.webUploader, queuedFiles = this.queuedFiles;
	                	$.each(files, function(index, file) {
                            if(ImageSuffix.indexOf(file.ext.toLowerCase()) < 0){
                                $.each(queuedFiles, function(index, item) {
                                    if(file.id == item.file.id){
                                        item.remove();
                                    }
                                });
                                return true;
                            }
	                		webUploader.makeThumb( file, function( error, src ) {
		                        if ( error ) {
		                        	Util.alert('不能预览');
		                            return;
		                        }
		                        _this.bind.images.push({
		                        	key: file.name,
		                        	src: src
		                        });
		                        _this.digest();
		                    }, 500, 300 );
						});
                        if(queuedFiles.length == 0)
                            Util.alert(ImageSuffixNotSupport);
	                	webUploader.upload();
	                }
	            });
			};
			updateSupport.fn.removeImg = function(resouceId) {
				var i = 0;
                $.each(this.bind.images, function(index, row){
                	if(row.resouceId == resouceId){
                		i = index;
                		return
                	}
                });
                this.bind.images.splice(i, 1);
                if(this.bind.images.length == 0){
                	this.bind.isOk = '';
                	this.$scope.updateVisitorForm.isOk.$setViewValue('');
                }
			};
			updateSupport.fn.save = function() {
				var _this = this;
				 _this.$scope.entity.projectCode =  _this.$scope.entity.projectCodes ;
				 _this.$scope.entity.personRole =  _this.$scope.entity.roleCode ;
				 _this.$scope.entity.jpgHead = $("#capturePhoto").attr('src');
				$.ajax({
                    url : getServer() + "/flea/hclv/person/saveOrUpdate.htmls",
                    data : {
                        'model' : JSON.stringify(_this.$scope.entity)
                    },
                    success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
                    	_this.$scope.grid.refresh();
                        Util.alert('成功');
                        _this.slidebars.close();
                    }
				});
			};
			return updateSupportInit;
		});